package cn.com.wxd.plugin.baiduueditor;

import java.io.IOException;
import java.util.HashMap;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.service.attach.AttachUtil;
import cn.com.wxd.service.freemarker.core.FreeMarker;
import cn.com.wxd.util.RequestUtil;
import freemarker.template.TemplateException;

/**
 * 用于生成uditor1.4的配置文件
 * @author ComXu
 *
 */
public class GenerateConfigJson {
	private static final Logger log = LoggerFactory.getLogger(GenerateConfigJson.class);

	private static final String UEDITORCONFIGPATH = "/static/util/plugins/ueditor1.4/jsp";
	private static final String UEDITORUPLOADPARAM = "ueditorattach";
	/**
	 * 生成ueditor配置文件
	 * @throws DocumentException 
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	public static void init() throws IOException, DocumentException, TemplateException{
		FreeMarker fm = new FreeMarker(RequestUtil.getRealPath() + UEDITORCONFIGPATH);
		//使用FreeMarker生成配置文件
		HashMap<String,String> map = new HashMap<>();
		String upsPath = AttachUtil.instance().getAttachConfByParamName(UEDITORUPLOADPARAM).getPath();
		map.put("PathFormat",upsPath);
		fm.processToFile("config.ftl",map,RequestUtil.getRealPath() + UEDITORCONFIGPATH + "/config"
				+ ".json");
	}
	/**
	 * 获取配置文件中对文件上传地址的配置
	 * @return
	 */
	public static String getUploadPath(){
		String upsPath="";
		try {
			upsPath = AttachUtil.instance().getAttachConfByParamName(UEDITORUPLOADPARAM).getPath();
		} catch (IOException | DocumentException e) {
            log.error("",e);
		}
		return upsPath;
	}
}
